package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.Vo.ProviderVo;
import com.khyr.dao.ProviderDao;
import com.khyr.entity.StockProvider;
import com.khyr.service.ProviderService;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: ProviderServiceImpl
 * @author: Zyl
 * @date: 2025/3/12 20:42
 * @Version: 1.0
 * @description:
 */
@Service
public class ProviderServiceImpl implements ProviderService {
@Autowired
ProviderDao providerDao;
    @Override
    //分页查询
    public R<Page<StockProvider>> findProvider(ProviderVo data) {
        QueryWrapper<StockProvider> queryWrapper=new QueryWrapper<>();
        if (data.getProviderName() != null) {
            queryWrapper.like("provider_name", data.getProviderName());
        }
        if (data.getContactName() != null) {
            queryWrapper.like("contact_name", data.getContactName());
        }
        if (data.getContactTel() != null) {
            queryWrapper.like("contact_tel", data.getContactTel());
        }
        if (data.getStatus() != null) {
            queryWrapper.eq("status", data.getStatus());
        }
        Page<StockProvider> page = new Page<>(data.getPageNum(), data.getPageSize());
        providerDao.selectPage(page, queryWrapper);
        return new R<>(200,"查询成功",page);
    }

    @Override
    public R addProvider(ProviderVo data) {
        int i = providerDao.insert1(data);
        if (i > 0) {
            return new R<>(200, "添加成功", null);
        } else {
            return new R<>(500, "添加失败", null);
        }
    }

    @Override
    //回显信息
    public R getProviderById(Integer providerId) {
        StockProvider stockProvider = providerDao.selectById(providerId);
        if (stockProvider != null) {
            return new R<>(200, "查询成功", stockProvider);
        } else {
            return new R<>(500, "查询失败", null);
        }
    }

    @Override
    public R updateProvider(ProviderVo data) {
        int i = providerDao.update1(data);
        if (i > 0) {
            return new R<>(200, "修改成功", null);
        } else {
            return new R<>(500, "修改失败", null);
        }

    }

    @Override
    public R deleteProviderByProviderIds(List<Integer> providerIds) {
        int i = providerDao.deleteProviderByProviderIds(providerIds);
        if (i > 0) {
            return new R<>(200, "删除成功", null);
        } else {
            return new R<>(500, "删除失败", null);
        }

    }

    @Override
    public R<List<StockProvider>> selectAllProvider() {
        QueryWrapper<StockProvider> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        List<StockProvider> list = providerDao.selectList(queryWrapper);
        if (list != null) {
            return new R<>(200, "查询成功", list);
        } else {
            return new R<>(500, "查询失败", null);
        }
    }
}
